"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:ProjectPaymentCtrl
 * @description
 * # ProjectPaymentCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller("ProjectPaymentCtrl", function (
    $scope,
    $rootScope,
    openApi,
    Util,
    $location,
    $routeParams,
    FormSubmit,
    localStorageService
  ) {
    var id = ($scope.process_id = $routeParams.id);
    // 选择款项类别
    $scope.costTypeList = [
      {
        key: "保证金",
        value: 3
      },
      {
        key: "押金",
        value: 4
      },
      {
        key: "缴纳",
        value: 5
      },
      {
        key: "成本",
        value: 6
      },
      {
        key: "收入",
        value: 7
      }
    ];
    // 选择发票类别
    $scope.invoiceTypeList = [
      {
        key: "空",
        value: ""
      },
      {
        key: "进项发票",
        value: "Xm_InputInvoice"
      },
      {
        key: "成本发票",
        value: "Finance_PaymentInvoice"
      }
    ];

    // 选择合同类别
    $scope.contractTypeList = [
      {
        key: "空",
        value: ""
      },
      {
        key: "通用合同",
        value: "Contract_CommonContract"
      },
      {
        key: "采购合同",
        value: "Xm_ContractPurchase"
      },
      {
        key: "租赁合同",
        value: "Xm_ContractRent"
      },
      {
        key: "分包合同",
        value: "Xm_ContractSubpackage"
      },
      {
        key: "其他合同",
        value: "Xm_ContractOther"
      }
    ];

    $scope.chooseContract = function () {
      var projectParams = {
        projectNo: $scope.param.projectNo,
        projectId: $scope.param.projectId,
        formKey: $scope.param.contractType,
        fieldName: "commonContractNo"
        // keyWrod: "Xm_ProjectPayment"
      };
      $scope.choosedata(projectParams);
    };
    $scope.chooseInvoice = function () {
      var projectParams = {
        projectId: $scope.param.projectId,
        formKey: $scope.param.invoiceType,
        fieldName: "paymentInvoiceNo"
        // keyWrod: "Xm_ProjectPayment"
      };
      $scope.choosedata(projectParams);
    };
    $scope.chooseContract1 = function () {
      var projectParams = {
        projectNo: $scope.param.projectNo,
        projectId: $scope.param.projectId,
        formKey: $scope.payPicker.contractType,
        fieldName: "commonContractNo"
        // keyWrod: "Xm_ProjectPayment"
      };
      $scope.choosedata(projectParams);
    };

    $scope.chooseInvoice1 = function () {
      var projectParams = {
        projectId: $scope.param.projectId,
        formKey: $scope.payPicker.invoiceType,
        fieldName: "paymentInvoiceNo"
        // keyWrod: "Xm_ProjectPayment"
      };
      $scope.choosedata(projectParams);
    };

    $scope.choosedata = function (projectParams) {
      if (!$scope.param.projectId) {
        Util.Toast("请先选择项目");
        return;
      }
      if ($scope.showPayPicker) {
        localStorageService.set("pickerdata", $scope.payPicker);
        closePicker();
      }
      // 清掉关联的编号
      Util.savaParamForReview($scope.param, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.chooseSk = function () {
      if (!$scope.param.projectId) {
        Util.Toast("请先选择项目");
        return;
      }
      if ($scope.showPayPicker) {
        localStorageService.set("pickerdata", $scope.payPicker || {});
        closePicker();
      }
      var projectParams = {
        projectId: $scope.param.projectId,
        formKey: "Xm_ProjectGathering",
        fieldName: "gatheringNo"
      }
      // 清掉关联的编号
      Util.savaParamForReview($scope.param, id);
      localStorageService.set("projectParams", projectParams);
      $location.path("/project/choosedata");
    };

    $scope.chooseProject = function () {
      // 将列表的数据序列化存入缓存以便回来时候使用
      Util.savaParamForReview($scope.param, id);
      $location.path("project/chooseproject");
    };

    // 款项类别大类更改时候小类清空
    $scope.costTypechange = function () {
      $scope.picker.deductName = "";
      $scope.getcostsubTypeList();
    };
    $scope.getcostsubTypeList = function () {
      var param = {};
      param.type = "项目款项小类";
      param.parent = $scope.costTypeList.filter(function (item) {
        return item.value == $scope.picker.deductType;
      })[0].key;
      Util.getKeyValue(param, function (data) {
        $scope.costsubTypeList = data.data;
      });
    };
    // 大类更改时候小类清空
    $scope.payTypechange = function () {
      $scope.param.payType2 = "";
      $scope.getgcksubTypeListA();
    };
    $scope.payTypechange2 = function () {
      $scope.payPicker.payType2 = "";
      $scope.getgcksubTypeList();
    };
    $scope.getgcksubTypeList = function () {
      var param = {};
      param.type = "工程款类型";
      param.parent = $scope.payPicker.payType1;
      Util.getKeyValue(param, function (data) {
        $scope.gcksubTypeList = data.data;
      });
    };
    $scope.getgcksubTypeListA = function () {
      var param = {};
      param.type = "工程款类型";
      param.parent = $scope.param.payType1;
      Util.getKeyValue(param, function (data) {
        $scope.gckTypeListA = data.data;
      });
    };
    $scope.showgckTypeObj = function () {
      var param = {};
      param.type = "工程款类型";
      Util.getKeyValue(param, function (data) {
        $scope.gckTypeList = data.data;
      });
    };
    $scope.contractTypechange = function () {
      $scope.param.commonContractNo = "";
    };
    $scope.invoiceTypechange = function () {
      $scope.param.paymentInvoiceNo = "";
    };
    $scope.contractTypechange1 = function () {
      $scope.payPicker.commonContractNo = "";
    };
    $scope.invoiceTypechange1 = function () {
      $scope.payPicker.paymentInvoiceNo = "";
    };
    // 选择报销单明细
    $scope.showCertificate = function () {
      $scope.showCerPicker = true;
      $rootScope.$emit("showmasker");
    };
    $scope.deletedItem = function (index) {
      $scope.param.deductList.splice(index, 1);
      $scope.costindex = -1;
    };
    $scope.addCertificate = function () {
      $scope.picker = $scope.picker || {};
      var check = false;
      check = checkPicker($scope.picker);
      if (!check) {
        return;
      }
      $scope.param.deductList.push($scope.picker);
      // 隐藏表单
      closePicker();
    };
    $scope.addPayItem = function () {
      $scope.payPicker = $scope.payPicker || {};
      var check = false;
      check = checkPayPicker($scope.payPicker);
      if (!check) {
        return;
      }
      $scope.param.paymentList.push($scope.payPicker);
      // 隐藏表单
      closePicker();
    };
    $scope.deletePayItem = function (index) {
      $scope.param.paymentList.splice(index, 1);
      $scope.payindex = -1;
    };

    var closePicker = function () {
      $scope.picker = {};
      $scope.payPicker = {};
      $rootScope.$emit("closemasker");
      $scope.showCerPicker = false;
      $scope.showPayPicker = false;
    };
    $scope.$on("closemasker", function () {
      closePicker();
    });
    $scope.closePicker = function () {
      closePicker();
    };
    $scope.swipe = function (index, type) {
      if (type) {
        $scope.payindex = index;
      } else {
        $scope.costindex = index;
      }
    };
    $scope.resetlist = function (type) {
      if (type) {
        $scope.payindex = -1;
      } else {
        $scope.costindex = -1;
      }
    };
    // 验证扣款明细是否输入完整
    var checkPicker = function (data) {
      if (!data.deductAmount) {
        Util.Toast("请填写扣款金额");
        return;
      }
      if (!data.deductType) {
        Util.Toast("请选择款项类型");
        return;
      }
      if (!data.deductRemark) {
        Util.Toast("请填写扣款事由");
        return;
      }
      return true;
    };
    // 验证付款明细是否输入完整
    var checkPayPicker = function (data) {
      if (!data.acceptAmount) {
        Util.Toast("请填写付款金额");
        return;
      }
      if (!data.payType1) {
        Util.Toast("请选择工程款大类");
        return;
      }
      if (!data.payType2) {
        Util.Toast("请选择工程款小类");
        return;
      }
      if (!data.acceptPerson) {
        Util.Toast("请填写收款人");
        return;
      }
      if (!data.acceptAccount) {
        Util.Toast("请填写收款人账号");
        return;
      }
      if (!data.acceptBank) {
        Util.Toast("请填写收款人开户行");
        return;
      }
      if (data.invoiceType) {
        if (!data.paymentInvoiceNo) {
          Util.Toast("请选择发票编号");
          return;
        }
      }
      if (data.contractType) {
        if (!data.commonContractNo) {
          Util.Toast("请选择合同编号");
          return;
        }
      }

      return true;
    };
    $scope.showPayment = function () {
      $rootScope.$emit("showmasker");
      $scope.showPayPicker = true;
    };

    $scope.choosePayment = function () {
      if (!$scope.param.projectName) {
        Util.Toast("请先选择工程名称");
        return;
      }
      var relModuleObj = {
        selectTab: "select_payment_invoice",
        propertyName: "paymentInvoiceNo",
        propertyValue: "fileNo",
        param: {
          contractNo: $scope.param.contractNo
        },
        selectTabContent: "发票编号"
      };
      // 清掉关联的编号
      var params = $scope.param;
      Util.savaParamForReview(params, id);
      Util.savaDataforsearch(relModuleObj);
      $location.path("finance/choosemultinfo");
    };


    $scope.showSearchPicker = function () {
      $scope.searchtitle = "收款人";
      $scope.apiurl = openApi.projectPaymentHistory;
      $scope.showSearchInput = true;
      $rootScope.$emit("showmasker");
    };
    $scope.handleSearch = function (data) {
      $scope.param.acceptPerson = data[0];
      $scope.param.acceptAccount = data[1];
      $scope.param.acceptBank = data[2];
      hideSearchPicker();
    };
    var hideSearchPicker = function () {
      $rootScope.$emit("closemasker");
      $scope.showSearchInput = false;
    };
    $scope.$on("closemasker", function () {
      hideSearchPicker();
    });

    $scope.submit = function (operationId) {
      $scope.param.deducts = angular.toJson($scope.param.deductList);
      $scope.param.payments = angular.toJson($scope.param.paymentList);
      // 底部类型end
      var options = {};
      // 流程id
      options.id = id;
      // 根据operationId判断是草稿还是提交
      options.operationId = operationId;
      options.urlParam = openApi.projectPayment;
      options.param = angular.copy($scope.param, {});
      delete options.param.deductList;
      delete options.param.paymentList;
      delete options.param.invoices;
      FormSubmit.submit(options, $scope);
    };
    $scope.validate = function () {
      if (!$scope.param.projectName) {
        Util.Toast("请选择项目名称");
        return;
      }
      if (!$scope.param.payReason) {
        Util.Toast("请输入付款事由");
        return;
      }
      if (!$scope.param.acceptAmount) {
        Util.Toast("请填写付款金额");
        return;
      }
      if (!$scope.param.payType1) {
        Util.Toast("请选择工程款大类");
        return;
      }
      if (!$scope.param.payType2) {
        Util.Toast("请选择工程款小类");
        return;
      }
      if (!$scope.param.acceptPerson) {
        Util.Toast("请填写收款人");
        return;
      }
      if (!$scope.param.acceptAccount) {
        Util.Toast("请填写收款人账号");
        return;
      }
      if (!$scope.param.acceptBank) {
        Util.Toast("请填写收款人开户行");
        return;
      }
      if (!$scope.validation.passvalidation) {
        Util.Toast($scope.validation.tip);
        return;
      }
      return true;
    };
    var init = function () {
      // 获取回显的详细信息
      Util.getDetailData(id, function (data) {
        $scope.param = data;
        $scope.param.deductList = $scope.param.deductList || [];
        $scope.param.paymentList = $scope.param.paymentList || [];
        // 获取项目信息
        var data = localStorageService.get("projectinfo");
        localStorageService.remove("projectinfo");
        $scope.param = Object.assign({}, $scope.param, data);
        $scope.showgckTypeObj();
        if ($scope.param.payType1) {
          $scope.getgcksubTypeListA();
        }
        // 获取项目关联数据
        var projectreldata = localStorageService.get("projectreldata");
        localStorageService.remove("projectreldata");
        // 如果有选择框则显示选择框
        var pickerdata = localStorageService.get("pickerdata");
        if (pickerdata) {
          localStorageService.remove("pickerdata");
          $scope.payPicker = Object.assign({}, pickerdata, projectreldata);
          // 删除合同id，不然提交到后台会报错
          delete $scope.payPicker.contractId;
          if (pickerdata.payType1) {
            $scope.getgcksubTypeList();
          }
          $scope.showPayment();
        } else {
          $scope.param = Object.assign({}, $scope.param, projectreldata);
        }
      });
    };
    // 初始化
    init();
  });
